//
//  ZPMineContinueVC.m
//  ZPIOSEducation
//
//  Created by 李伟 on 2017/1/15.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPMineContinueVC.h"
#import "MineContinueCell.h"
#import "ComfirmOrderViewController.h"

@interface ZPMineContinueVC () <UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray  *dataArray;
    NSInteger  selectIndex;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (weak, nonatomic) IBOutlet UIButton *xuBaoBtn;

@end

@implementation ZPMineContinueVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"续报";
    self.xuBaoBtn.layer.cornerRadius = 4.;
    self.xuBaoBtn.layer.masksToBounds = YES;
    self.xuBaoBtn.userInteractionEnabled = NO;
    
    selectIndex = -1;
    dataArray = [[NSMutableArray  alloc] init];

    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableHeaderView = [self  createTableHeaderView];
    
    [self  requestData];
}

- (UIView  *)createTableHeaderView
{
    UIView  *tempView = [[UIView  alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
    tempView.backgroundColor = [UIColor  whiteColor];
    
    UILabel  *titleLab = [[UILabel  alloc] initWithFrame:CGRectMake(12, 16, 300, 20)];
    titleLab.text = @"请选择要续报的班级";
    titleLab.textColor = RGBACOLOR(33, 164, 235, 1.);
    titleLab.font = [UIFont  systemFontOfSize:14.];
    titleLab.backgroundColor = [UIColor  clearColor];
    [tempView  addSubview:titleLab];
    
    return tempView;
}

- (IBAction)xuBaoButtonClick:(id)sender
{
    if (selectIndex < 0) {
        return;
    }
    NSDictionary  *tempDic = [dataArray  objectAtIndex:selectIndex];
    [self  showHudithString:@"加载中..."];
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:
                               [[ZPAuthStore  sharedZPAuthStore].userToDic objectForKey:@"memberId"],@"memberId",
                               [[ZPAuthStore  sharedZPAuthStore].userToDic objectForKey:@"stuId"],@"stuId",
                               [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"classId"]],@"classId",
                               [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"courseId"]],@"courseId",
                               nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIOrder@"/orders/initorder.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"======%@",responseObject);
        [self  hiddenHud];
        
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            NSDictionary *result = [responseObject objectForKey:@"result"];
            ComfirmOrderViewController *viewController = [[ComfirmOrderViewController alloc] init];
            viewController.infoDic = [result objectForKey:@"result"];
            viewController.courseId = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"courseId"]];
            viewController.classId = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"classId"]];
            viewController.priceName = [[result objectForKey:@"result"] objectForKey:@"priceName"];
            [self.navigationController pushViewController:viewController animated:YES];
            
        }else {
            [ZPPromptView  showMessage:[responseObject  objectForKey:@"codeMsg"]];
            [self.navigationController  popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        
    }];
}

#pragma mark - 网络相关
- (void)requestData
{
    [self  showHudithString:@"加载中..."];
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[NSString  stringWithFormat:@"%@",self.classId],@"classId", nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIUC@"adjust/getconclasslist.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"======%@",responseObject);
        [self  hiddenHud];
        
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            if ([[responseObject  objectForKey:@"result"] isKindOfClass:[NSArray  class]]) {
                [dataArray  removeAllObjects];
                [dataArray  addObjectsFromArray:[responseObject  objectForKey:@"result"]];
                [self.mainTableView  reloadData];
            }else {
                [ZPPromptView  showMessage:@"获取数据失败"];
            }
            
        }else {
            [ZPPromptView  showMessage:[responseObject  objectForKey:@"codeMsg"]];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        
    }];
}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray  count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray  *scheduleList = [[dataArray  objectAtIndex:indexPath.row] objectForKey:@"scheduleList"];
    NSInteger  count = [scheduleList  count];
    if (count > 0) {
        if (count % 6 == 0) {
            return 174 + 48 * (scheduleList.count / 6);
        }else {
            return 174 + 48 * ((scheduleList.count / 6) + 1);
        }
    }else {
        return 174 + 48;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MineContinueCell";
    MineContinueCell  *cell = (MineContinueCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineContinueCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    [cell  setContentCellWithDic:[dataArray  objectAtIndex:indexPath.row]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectIndex = indexPath.row;
    [self.xuBaoBtn  setBackgroundColor:RGBACOLOR(33, 164, 235, 1.)];
    self.xuBaoBtn.userInteractionEnabled = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
